<?php

/**
 * @filename common.php 
 * @encoding UTF-8 
 * @datetime 2016-7-18  14:11:10
 * @version 1.0
 * @Description 自定义公用函数
 */
\think\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");

\think\Validate::extend('captcha', function ($value, $id = "") {
    return captcha_check($value, $id, (array) \think\Config::get('captcha'));
});

\think\Validate::setTypeMsg('captcha', '验证码错误!');

function success($info = '', $data = array()) {
    http_response_code(200);
    $d['status'] = 'success';
    $d['message'] = $info ? $info : 'success';
    $d['data'] = $data;
    return_json($d);
}

/**
 * 失败返回
 * @Date   2015-09-25
 * @param  string $message 错误信息
 * @return json           json对象
 */
function error($message = '', $type = "") {
    http_response_code(404);
    $error['type'] = $type;
    $error['status'] = 'error';
    $error['message'] = $message ? $message : 'error';
    return_json($error);
}

/**
 * 返回json对象
 * @Date   2015-09-25
 * @param  array $data 数组数据
 * @return json           json对象
 */
function return_json($data) {
    echo is_array($data) ? json_encode($data) : $data;
    exit;
}

/**
 * @param string $id
 * @param array  $config
 * @return \think\Response
 */
function captcha($id = "", $config = []) {
    $captcha = new \think($config);
    return $captcha->entry($id);
}

/**
 * @param $id
 * @return string
 */
function captcha_src($id = "") {
    return \think\Url::build('/Admin/Captcha' . ($id ? "/{$id}" : ''), '', false);
}

/**
 * @param $id
 * @return mixed
 */
function captcha_img($id = "") {
    $url = \think\Url::build('/Admin/Captcha' . ($id ? "/{$id}" : ''), '', false);
    $o = "this.src='" . $url . "'+'?a='+Math.random();";
    return '<img style="width:100%" src="' . captcha_src($id) . '"  alt="captcha" onclick="' . $o . '" />';
}

/**
 * @param        $value
 * @param string $id
 * @param array  $config
 * @return bool
 */
function captcha_check($value, $id = "", $config = []) {
    $captcha = new \think\Captcha($config);
    return $captcha->check($value, $id);
}

//表格空数据提示
function table_empty_list($list, $text = '暂无数据!', $colspan = "15") {
    if (is_array($list)) {
        if (!$list) {
            return '<tr><td colspan="' . $colspan . '" class="text-center text-gray">' . $text . '</td></tr>';
        }
    } else {
        if ($list->total() === 0) {
            return '<tr><td colspan="' . $colspan . '" class="text-center text-gray">' . $text . '</td></tr>';
        }
    }
}

//开始时间-结束时间拆分成时间戳
function daterangepicker_to_int($text, $space = '到') {
    $time = explode($space, $text);
    $time[0] = strtotime(trim($time[0]));
    $time[1] = strtotime(trim($time[1]));
    return $time;
}

function getPageSize() {
    $config_sys = cache("config_sys");
    if ($config_sys && $config_sys['system_pagesize']) {
        return $config_sys['system_pagesize'];
    }
    return 10;
}

function getUserTypeName($user_type) {

    switch ($user_type) {
        case "user":

            echo "普通会员";
            break;
        case "agent":

            echo "代理会员";
            break;
        case "admin":
            echo "管理员";
            break;
        case "superuser":

            echo "超级管理员";
            break;

        default:
            break;
    }
}
